//
//  TopicViewController.m
//  A段项目,漫画
//
//  Created by dllo on 15/4/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "TopicViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "MJRefresh.h"
@interface TopicViewController ()
@property(nonatomic,retain)NSMutableArray *arrAction;
@property(nonatomic,retain)NSMutableArray *classArr;
@property(nonatomic,assign)NSInteger page;
//动画
@property(nonatomic, retain)UIView *witeView;
@property(nonatomic, retain)NSMutableArray *witeImageArr;
@property(nonatomic, retain)UIImageView *witeImageView;
@property(nonatomic, retain)UIProgressView *myProgress;
@property(nonatomic, retain)UILabel *myLabel;
@end

@implementation TopicViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrAction = [NSMutableArray array];
        self.classArr = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}
- (void)createWiteImage{
    self.witeImageArr = [NSMutableArray array];
    NSArray *names = @[@"735d4677ec7cdd522281cfbddd29ac71－1（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－2（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－3（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－4（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－5（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－6（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－7（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－8（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－9（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－10（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－11（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－12（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－13（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－14（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－15（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－16（被拖移）.tiff"];
    for (NSString *name in names) {
        UIImage *image = [UIImage imageNamed:name];
        [self.witeImageArr addObject:image];
    }
    self.witeImageView.animationImages = self.witeImageArr;
    //设置播放时间
    self.witeImageView.animationDuration = 0.5;
}
- (void)createAvtivity{
    self.witeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.witeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.witeView];
    [self.witeView release];
    
    CGFloat bili = 222.0 / 271;
    self.witeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, WIDTH / 1.5 * bili)];
    self.witeImageView.center = CGPointMake(WIDTH / 2, HEIGHT / 3);
    [self.witeView addSubview:self.witeImageView];
    [self.witeImageView release];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, 30)];
    self.myLabel.center = CGPointMake(WIDTH / 2, HEIGHT / 3 + WIDTH / 1.5 * bili / 2 + 10);
    self.myLabel.text = @"loading..........";
    [self.witeView addSubview:self.myLabel];
    self.myLabel.textAlignment = NSTextAlignmentCenter;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createAvtivity];
    [self createWiteImage];
    [self creatTopicView];
    
    [self creatActionClassView];
    
    [self PleaseData];
    
    [self PleaseActionClassData];
    
    [self addFooter];
    
    [self addHeader];
    
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"强力推荐",@"最全分类"]];
    self.navigationItem.titleView = seg;
    seg.tintColor = [UIColor purpleColor];
    seg.backgroundColor = [UIColor whiteColor];
    seg.selectedSegmentIndex = 0;
    [seg addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
    [seg release];
    [self.view bringSubviewToFront:self.topicTableView];
    
    [self.view bringSubviewToFront:self.witeView];
    //播放动画
    [self.witeImageView startAnimating];
    
}

#pragma mark 分类数据请求
-(void)PleaseActionClassData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=channelgroupNew" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *arr = responseObject;
        NSMutableDictionary *tempDic = [arr lastObject];
        NSMutableArray *listArr = [tempDic objectForKey:@"list"];
        for (NSMutableDictionary *Dic in listArr) {
            ActionClass *act = [[ActionClass alloc]init];
            [act setValuesForKeysWithDictionary:Dic];
            [self.classArr addObject:act];
            [act release];
        }
        [self.actionView reloadData];
        NSLog(@"数据请求成功");
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"请求失败");
    }];
}
#pragma mark 最全分类
//chuangjian分类视图
-(void)creatActionClassView
{
    UICollectionViewFlowLayout *flow = [UICollectionViewFlowLayout alloc];
    flow.itemSize = CGSizeMake(WIDTH *0.265, 130);
    
    flow.minimumInteritemSpacing =12;
    flow.minimumLineSpacing = 20;
    flow.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.actionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:flow];
    self.actionView.backgroundColor = [UIColor whiteColor];
    self.actionView.layer.borderWidth = 1;
    self.actionView.layer.borderWidth = 10;
    [self.view addSubview:self.actionView];
    [_actionView release];
    
    self.actionView.delegate = self;
    self.actionView.dataSource = self;
    
    //注册
    [self.actionView registerClass:[AvtionClassCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.navigationController.navigationBar.translucent = NO;
}

//两个协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.classArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    AvtionClassCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    ActionClass *act = self.classArr[indexPath.row];
    NSURL *url = [NSURL URLWithString:act.pic];
    [cell.picImageView sd_setImageWithURL:url];
    cell.nameLabel.text = act.name;
    return cell;
}




//创建推荐视图
-(void)creatTopicView
{
    
    
    
    //视图
    self.topicTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) style:UITableViewStylePlain];
    self.topicTableView.rowHeight = 200;
    [self.view addSubview:self.topicTableView];
    [_topicTableView release];
    
    //代理
    self.topicTableView.delegate = self;
    self.topicTableView.dataSource = self;
}

-(void)click:(UISegmentedControl *)seg
{
    NSInteger index = seg.selectedSegmentIndex;
    switch (index) {
        case 0:
            [self.view bringSubviewToFront:self.topicTableView];
            break;
            case 1:
            [self.view bringSubviewToFront:self.actionView];
        default:
            break;
    }
    
}
#pragma mark 实现协议的两个方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrAction.count;
}

//table
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *reuse = @"reuse";
    TopicCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[TopicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    
    if (indexPath.row < self.arrAction.count - 1) {
        Topic *top = self.arrAction[indexPath.row + 1];
        NSURL *url = [NSURL URLWithString:top.pic];
        [cell.ActionView sd_setImageWithURL:url];
        cell.titleLabel.text = top.name;
        cell.timeLabel.text = top.lastup;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return cell;
    
    Topic *top = self.arrAction[indexPath.row];
    NSURL *url = [NSURL URLWithString:top.pic];
    [cell.ActionView sd_setImageWithURL:url];
    cell.titleLabel.text = top.name;
    cell.timeLabel.text = top.lastup;
    if (indexPath.row == 2) {
        NSLog(@"结束动画");
        [self.witeImageView stopAnimating];
        [self.view bringSubviewToFront:self.topicTableView];
    }
    return cell;
}

#pragma mark 跳转动画分类写一个页面
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ActionClassView *acv = [[ActionClassView alloc]init];
    ActionClass *act = self.classArr[indexPath.row];
    acv.video_id = act.id;
    [self.navigationController pushViewController:acv animated:YES];
    [acv release];
}


#pragma mark 点击Label跳转到topicView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ActionDViewController *advc = [[ActionDViewController alloc]init];
    Topic *ad = self.arrAction[indexPath.row + 1];
    advc.video_id = ad.id;
    [self.navigationController pushViewController:advc animated:YES];
    [advc release];
}

#pragma mark 数据请求
-(void)PleaseData
{
    
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=newIndex&tab=1&page=%ld&limit=15&hasTopic=1",self.page];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"list"];
        //        [arr removeObjectAtIndex:1];
        for (NSMutableDictionary *tempDic in arr) {
            Topic *top = [[Topic alloc]init];
            [top setValuesForKeysWithDictionary:tempDic];
            [self.arrAction addObject:top];
            [top release];
        }
        [self.topicTableView reloadData];
        NSLog(@"------数据请求成功");
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"数据请求失败");
    }];
    
}
//刷新
-(void)addHeader
{
    [self.topicTableView addHeaderWithCallback:^{
        
        self.page = 1;
        
        [self.arrAction removeAllObjects];
        [self PleaseData];
        [self.topicTableView reloadData];
        [self.topicTableView headerEndRefreshing];
        
    }];
    [self.topicTableView headerBeginRefreshing];
}
//加载
-(void)addFooter
{
    [self.topicTableView addFooterWithCallback:^{
        self.page ++;
        [self PleaseData];
        [self.topicTableView reloadData];
        [self.topicTableView footerEndRefreshing];
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
